﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SaleWebsite.Models;
using System.Data;

namespace SaleWebsite.Controllers
{
    public class Product_TypeController : Controller
    {
        //
        // GET: /Product_Type/

        public ActionResult Index()
        {

            return View(Group.GetAll());
        }
        public ActionResult insert(string name,ulong group_id,string group_name)
        {

            Product_Type item = new Product_Type(0, name,group_id);
            ulong id = item.Insert();
            if (id>0)
            {
                item.Id = id;
                item.Group_name = group_name;
            }
            
            return PartialView("product_type_row", item);


        }
        public ActionResult GetAllProduct_type()
        {

            return PartialView("product_view_all", Convert_All(Product_Type.GetAll()));
        }
        private List<Product_Type>Convert_All(DataSet data)
        {
            List<Product_Type> list = new List<Product_Type>();
            foreach ( DataRow item in data.Tables[0].Rows)
            {
                list.Add(convert(item));
            }
            return list;
        }
        private Product_Type convert(DataRow row)
        {
            return new Product_Type() {Id=ulong.Parse(row["product_id"].ToString()),Name=row["product_name"].ToString(),Group_Id=ulong.Parse(row["group_id"].ToString()),Group_name=row["group_name"].ToString() };
        }
        public JsonResult delete(ulong id)
        {
            Product_Type item = new Product_Type(id, "", 0);
            if (item.Delete() > 0)
            {
                return Json(new { status = "success" });
            }
            else
            {
                return Json(new { status = "not" });
            }

        }
        public JsonResult Save_Product_Type(ulong id, string name,ulong group_id)
        {
            try
            {
                Product_Type item = new Product_Type(id, name,group_id);
                if (item.Save() > 0)
                {
                    return Json(new { status = "success" });
                }
                else
                {
                    return Json(new { error = "update Faile" });
                }
            }
            catch (System.Exception ex)
            {
                return Json(new { error = ex.Message });
            }
        }

    }
}
